<?php
	/**
	* 
	*/
	class Main extends CI_Controller
	{
		
		public function __construct()
		{
			parent::__construct();
			$this->load->helper('url');
			$this->load->model('question_model');
			$this->load->model('result_model');
			$this->load->model('activity_model');
			$this->load->model('user_model');
			$this->load->library('session');
			$this->load->library('Ybglobals');
			$this->load->library('Configinyb');
			$this->load->library('Ybopenapi');
			$this->load->library('Authorize',$this->configinyb->configinyb());
		}
		//如果没有设置session就回到登陆页面
		public function sessionjudge(){
 			if($this->session->has_userdata('id')){}
 			else $this->ybapi();
		}
		public function ybreaddress(){
			$tokenarr=$this->authorize->querytoken($_GET['code']);
			if(isset($tokenarr['userid'])){
				$userid=$tokenarr['userid'];
				$token=$tokenarr['access_token'];//授权凭证
				$accesstoken=array('access_token'=>$token);
				$url='user/verify_me';
				$usermessage=$this->Ybopenapi->request($url,$accesstoken,false,true);//校方认证信息
				if(isset($usermessage['yb_userid'])){
					$yb_userid=$usermessage['yb_userid'];//获得学号
					$this->session->set_userdata('id',$yb_userid);//存id到session中，利用session判断管理员
					$masterdata=$this->user_model->masterjudge();
					foreach ($masterdata->result_array() as $row)
					{
						if($row['userId']==$this->session->userdata('id')){
					 		alert("管理员");
							return 1;//是管理员,接管理员要去的界面
						}
						else{
				 			alert("不是管理员");
				 			return 0;
				 		}//不是管理员,接普通用户界面
					}
				}
				else{
					var_dump($usermessage);
				}
			}
			else{//出错
				var_dump($tokenarr);
			}
			// $res = $this->ybopenapi->request('oauth/revoke_token', array('client_id'=>$this->ybopenapi->getConfig('appid'),'access_token'=>$this->ybopenapi->getConfig('token')), true); //清除用户授权
		}
		public function ybapi()
		{
			$backurl=$this->authorize->forwardurl();
		header('location: '.$backurl);
		//exit();
		}
		public function activityRelease()
		{
			$this->sessionjudge();
			$this->load->view('activity_release');
		}
		public function registrationRelease()
		{
			$this->sessionjudge();
			$this->load->view('registration_release');
		}

		public function addActInfo($vcUserId)
		{
			$this->sessionjudge();
			$data=$this->input->post();
			$data['vcUserId'] = $vcUserId;
			//var_dump($data);
			$this->activity_model->insertActInfo($data);
			$this->load->view("admin/pWeb");
		}
		public function showActInfo($pages=1)//显示所有内容
		{
			$this->sessionjudge();
			$dataBase['content'] = $this->activity_model->getActInfo();
			$num=$this->activity_model->getActInfoNum();
			for($i=($pages-1)*10;($i<$pages*10)&&$i<$num;$i++)
			{
				$data['content'][$i%10]=$dataBase['content'][$i];
			}
			//var_dump($data);
			echo json_encode($data);
		}
		public function home()
		{
			$this->load->view("normal/directory");
		}
		public function showOneActInfo($cActId)//显示单个内容
		{
			$this->sessionjudge();
			$data['activity_item']=$this->activity_model->getActInfo($cActId);
			//var_dump($data);
			echo json_encode($data);
		}
		public function content($cActId)
		{
			$this->sessionjudge();
			$data['cActId']=$cActId;
			$this->load->view("normal/content",$data);
		}
		public function showApply($id)//显示问卷
		{
			$this->sessionjudge();
			$data['apply']=$this->question_model->getAnsInfo($id);
			$data['activity_item']=$this->activity_model->getActInfo($id);
			echo json_encode($data);
			//$this->load->view("normal/signUp",$data);
		}
		public function apply($cActId)//用showApply显示，数据已传过去
		{
			$this->sessionjudge();
			$data['cActId']=$cActId;
			$this->load->view("normal/signUp",$data);
		}
		public function addQuesInfo()//添加问卷
		{
			$this->sessionjudge();
			$arr = $_POST['arr'];
			$arr = json_decode($arr,true);
			$data['iActId'] = 1;
			$len = count($arr) - 1;
			for ($i=0; $i < $len; $i++) { 
				$data['iQuizNo'] = $arr[$i+1]['num'];
				//var_dump($arr[$i+1]);
				$data['vcQues'] = $arr[$i+1]['title'];
				//可以开循环存入选项，更耗时
				$data['vcAns1'] = $arr[$i+1]['desc'][0];
				$data['vcAns2'] = $arr[$i+1]['desc'][1];
				$data['vcAns3'] = $arr[$i+1]['desc'][2];
				$data['vcAns4'] = $arr[$i+1]['desc'][3];
				$data['iType'] = $arr[$i+1]['type'];
				$this->question_model->insertAns($data);
				var_dump($data);
			}
		}
		public function addResult()//添加问卷结果
		{
			$this->sessionjudge();
			$i=1;
			$data=$this->input->post();
			$date['apply']=$this->question_model->getAnsInfo(1);//需要前端给
			$arr['iActId']=1;//需要前端给
			$arr['vcUserId ']=1;//需要前端给
			foreach($data as $data_item)
			{
				$str="vcQues".$i;
				$vaule="";
				if($date['apply'][$i-1]['iType']=="check")
				{
					$value="";
					foreach ($data_item as $item) {
						$value=$value.",".$item;
					}
				}
				else
				{
					$value=$data_item;
				}
				$arr[$str]=$value;
				$i++;
			}
			$this->result_model->insertResult($arr);
			//var_dump($arr);
		}
		public function showActInfoByAdmin($MasterId,$pages = 1)//根据创建者id显示其所创建的活动
		{
			$this->sessionjudge();
			$data['content'] = $this->activity_model->getActInfoByMaster($MasterId);
			//var_dump($data);
			echo json_encode($data);
		}
		public function pWebAdmin()
		{
			$this->sessionjudge();
			$this->load->view('admin/pWeb');
		}
		public function showActInfoByUser($UserId)//根据的报名者id显示他参加的所有活动
		{
			$this->sessionjudge();
			$dataBase['activity'] = $this->result_model->getActId($UserId);
			$num1 = $this->result_model->getActNum($UserId);
			for ($i = 0; $i < $num1; $i++) { 
				$data['content'][$i] = $this->activity_model->getActInfo($dataBase['activity'][$i]['iActId']);
			}
			//var_dump($data);
			echo json_encode($data);
		}
		public function pWebNormal()
		{
			$this->sessionjudge();
			$this->load->view('normal/pWeb');
		}
		
	}

?>